<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>服务管理</el-breadcrumb-item>
      <el-breadcrumb-item>服务列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <template>
        <div style="margin-bottom: 20px;font-size: 22px;color: red;">
          <span>{{houseData.address}}</span>
          <el-divider direction="vertical"></el-divider>
          <span>{{houseData.area}}㎡</span>
          <el-divider direction="vertical"></el-divider>
          <span>{{houseData.style_text}}</span>
        </div>
      </template>
      <div class="demo-image">
        <div class="block">
          <el-image style="width: 100px; height: 100px" :src="houseData.thumbnail_path" fit="fit"></el-image>
        </div>
      </div>
      <!-- 搜索查询 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <div>
            <el-input v-model="queryParam.name" placeholder="请输入内容" class="input-with-select" clearable></el-input>
          </div>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="handleSizeChange(queryParam.pageSize)">查询</el-button>
          <el-button type="primary" @click="houseRoomFormVisible = true">添加房间</el-button>
        </el-col>
      </el-row>
      <!-- table表格数据 start -->
      <template>
        <el-table :data="houseRoomDataList" style="width: 100%" max-height="570">
          <!-- 数据表 -->
          <el-table-column fixed prop="name" label="房间名称" width="150">
          </el-table-column>
          <el-table-column prop="describe" label="描述" width="120">
          </el-table-column>
          <el-table-column prop="thumbnail" label="封面图" width="200">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="140">
            <!-- 操作 -->
            <template slot-scope="scope">
              <!-- <el-button size="mini" type="warning" icon="el-icon-view" @click="addRoom" circle></el-button> -->
              <el-button size="mini" type="warning" icon="el-icon-edit" @click="editHouseRooForm(scope.row)" circle></el-button>
              <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteHouseRoom(scope.$index,scope.row.id)"
                circle></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- table表格数据 end -->
      <!-- 分页  start-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryParam.currentPage"
        :page-sizes="[10, 20, 30, 40]" :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="totalResult">
      </el-pagination>
      <!-- 分页  end> -->
    </el-card>
    <!-- 表单 start                                               -->
    <el-dialog title="案例" :visible.sync="houseRoomFormVisible" @close="closehouseRoomForm" width="30%" center>
      <!-- :rules="houseFormRules" -->
      <el-form :model="houseRoomForm" ref="houseRoomFormRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="房间名称" required prop="name">
          <el-input v-model="houseRoomForm.name"></el-input>
        </el-form-item>
        <el-form-item label="描述" required prop="describe">
          <el-input type="textarea" style="height: 150px;" v-model="houseRoomForm.describe"></el-input>
        </el-form-item>
        <!-- 图片上传   -->
        <el-form-item label="图片" required prop="thumbnail">
          <template>
            <el-upload class="upload-demo" accept="image/*" :data="uploadParams.uploadData" :action="uploadParams.uploadFileUrl"
              :headers="uploadParams.uploadHeaders" list-type="picture" :file-list='houseRoomForm.thumbnail_arr'
              :on-success="uploadSuccess" :on-preview="uploadPreview" :on-remove="uploadRemove" :limit="1" :on-exceed="handleExceed">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </template>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button v-if="operate === 'add'" @click="resethouseRoomForm">重置</el-button>
        <el-button type="primary" @click="updateOrCreatehouseRoomForm">确认</el-button>
      </span>
    </el-dialog>
    <!-- 表单 end -->
    <!-- 图片预览 -->
    <el-dialog title="图片预览" :visible.sync="imagePreviewVisible" width="50%">
      <div class="demo-image">
        <div class="block">
          <el-image style="width: 100%; height: 100%" :src="previewPath" fit="fill"></el-image>
        </div>H
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getUploadParamsApi,
    getHouseApi,
    getHouseRoomListApi,
    deleteHouseRoomApi,
    updateOrCreateHouseRoomApi
  } from '../request/api.js'
  export default {
    data() {
      return {
        houseRoomDataList: [],
        houseData: {},
        totalResult: 0,
        queryParam: {
          name: '',
          currentPage: 1,
          pageSize: 10
        },
        // 表单操作
        operate: 'add',
        houseRoomFormVisible: false,
        houseRoomForm: {},
        houseRoomFormTem: {
          name: null,
          house: null,
          describe: '',
          thumbnail: ''
        },
        // 文件上传操作
        uploadParams: {},
        // 预览图片
        imagePreviewVisible: false,
        previewPath: ''
      }
    },
    created() {
      this.getHouseRoomList()
      // 浅拷贝值
      this.houseRoomForm = JSON.parse(JSON.stringify(this.houseRoomFormTem))
      // 获取文件上传的参数配置
      this.uploadParams = getUploadParamsApi('house')
      const houseId = this.$route.params.house_id
      this.houseRoomForm.house = houseId
      this.getProjectCase(houseId)
      // }
    },
    methods: {
      // 获取房屋信息
      async getProjectCase(houseId) {
        const {
          data: res
        } = await getHouseApi(houseId)
        this.houseData = res.data
      },
      // 获取对应的所有的房间
      async getHouseRoomList() {
        const {
          data: res
        } = await getHouseRoomListApi(this.queryParam)
        console.log(res)
        this.houseRoomDataList = res.data.dataList
        this.totalResult = res.data.totalResult
      },
      // 分页事件
      handleSizeChange(newSize) {
        this.queryParam.pageSize = newSize
        this.queryParam.currentPage = 1
        this.getHouseRoomList()
      },
      handleCurrentChange(newPage) {
        this.queryParam.currentPage = newPage
        this.getHouseRoomList()
      },
      async deleteHouseRoom(index, id) {
        // 删除
        const data = await deleteHouseRoomApi(id)
        if (data.status === 200 && data.data.status) {
          this.$message.success('删除成功！')
          this.houseRoomDataList.splice(index, 1)
        }
      },
      editHouseRooForm(room) {
        this.houseRoomForm = room
        this.operate = 'edit'
        this.houseRoomFormVisible = true
      },
      updateOrCreatehouseRoomForm() {
        this.$refs.houseRoomFormRef.validate(async valid => {
          if (valid) {
            const {
              data: res
            } = await updateOrCreateHouseRoomApi(this.houseRoomForm, this.operate)
            console.log(res)
            if (res.status) {
              var message = this.operate === 'add' ? '添加成功！' : '编辑成功！'
              this.$message.success(message)
              this.houseRoomFormVisible = false
              this.operate = 'add'
              this.getHouseRoomList()
            } else {
              this.$message.error('操作失败！')
            }
          }
        })
      },
      // 重置表单
      resethouseRoomForm() {
        this.houseRoomForm = JSON.parse(JSON.stringify(this.houseRoomFormTem))
        this.$refs.houseRoomFormRef.resetFields()
      },
      // 关闭表单
      closehouseRoomForm() {
        this.resethouseRoomForm()
        this.houseRoomFormVisible = false
      },
      // 文件上传成功回调
      uploadSuccess(response) {
        if (response.status) {
          this.houseRoomForm.thumbnail = response.data.id
        }
      },
      // 移除文件
      uploadRemove(file) {
        this.houseRoomForm.thumbnail = null
      },
      // 预览图片
      uploadPreview(file) {
        this.imagePreviewVisible = true
        this.previewPath = file.url
      },
      // 文件超出个数限制时的钩子
      handleExceed(files, fileList) {
        this.$message.warning('只允许上传一图片，请删除后重新上传')
      }
    }
  }
</script>

<style>
</style>
